/*jslint node:true*/
'use strict';
var buf1 = new Buffer(5);

var buf2 = new Buffer([10, 20, 30, 40, 50]);

var buf3 = new Buffer("utf-8 是默认的编码方式，此外它同样支持以下编码：ascii, utf8, utf16le, ucs2, base64 和 hex。", 'utf-8');

var buf4 = new Buffer(256);

var buf5 = new Buffer('ascii');

//write buffer
var len = buf4.write("utf-8 是默认的编码方式，此外它同样支持以下编码：ascii, utf8, utf16le, ucs2, base64 和 hex。");
console.log('write byte:' + len);

//read buffer
console.log('buf3 read:');
console.log(buf3.toString('ascii'));
console.log(buf3.toString('ascii', 0, 12));
console.log(buf3.toString('utf8', 0, 12));
console.log(buf3.toString(undefined, 0, 12));

//buffer toJson
var json = buf5.toJSON(buf5);
console.log(json);

//buffer concat
console.log('concat buf5 and buf5:' + Buffer.concat([buf5, buf5]).toString());

//buffer compare
console.log('compare buf3 to buf5:' + buf3.compare(buf5));
console.log('compare buf5 to buf3:' + buf5.compare(buf3));
console.log('compare buf3 to buf3:' + buf3.compare(buf3));
console.log('compare buf2 to buf3:' + buf2.compare(buf3));

//buffer copy
buf5.copy(buf1);
console.log('copy buf5 to buf1,buf1 value:' + buf1.toString('utf8'));

//buffer slice
var buf6 = buf5.slice(0, 2);
console.log('slice buf5 to buf6, buf6 value:' + buf6.toString('utf8'));

//buffer length
console.log('buf3 length:' + buf3.length);

console.log('program finish');